<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="一级分类" prop="cid">
        <el-select v-model="queryParams.cid" placeholder="请选择一级分类" @change="oneQuery">
          <el-option
            v-for="item in classifyData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类" prop="cid2">
        <el-select v-model="queryParams.cid2" placeholder="请选择二级分类" filterable @change="handleQuery">
          <el-option
            v-for="item in classifyData2"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>




      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams.goodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建账号" prop="createUserName">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="请输入创建账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['video:DGoods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DGoodsList"  @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="50" align="center" />
     <el-table-column label="商品ID" align="center" prop="id" />
      <el-table-column label="一级分类" align="center" prop="classifyName" />
      <el-table-column label="二级分类" align="center" prop="classifyName2" />
      <el-table-column label="商品名称" align="center" prop="goodName" />
<!--      <el-table-column label="商品图片" align="center" prop="imageUrl" />-->
      <el-table-column prop="imageUrl" label="商品图片" width="90"  align="center">
        <template slot-scope="scope">
          　　　    <img :src="scope.row.imageUrl" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column label="视频下载路径" align="center" prop="videoUrl" />
      <el-table-column label="售价" align="center" prop="sellPrice" />
      <el-table-column label="试看时间(秒)" align="center" prop="testTime" />
      <el-table-column label="商品状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '1'">启用</span>
          <span v-if="scope.row.status == '2'">禁用</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="一级分类" prop="cid">
          <el-select v-model="form.cid" placeholder="请选择一级分类" @change="oneHandle">
            <el-option
              v-for="item in classifyData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" prop="cid2">
          <el-select v-model.trim="form.cid2" filterable placeholder="请选择二级分类" @change="twoHandle">
            <el-option
              v-for="item in classifyAddData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="form.goodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品图片" prop="imageUrl">
<!--          <el-input v-model="form.imageUrl" type="textarea" placeholder="请商品图片路径" />-->
          <image-upload v-model="form.imageUrl"/>
        </el-form-item>
        <el-form-item label="视频" prop="videoUrl">
          <el-input v-model="form.videoUrl" placeholder="请输入视频链接" />
<!--          <file-upload v-model="form.videoUrl"/>-->
        </el-form-item>
        <el-form-item label="售价" prop="sellPrice">
          <el-input v-model="form.sellPrice" placeholder="请输入售价" />
        </el-form-item>

        <el-form-item label="试看时间(秒)" prop="testTime">
          <el-input v-model="form.testTime" placeholder="请输入试看时间" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.good_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDGoods,
  getDGoods,
  delDGoods,
  addDGoods,
  updateDGoods,
  updateDGoodsBach, listSoldDGoods
} from "@/api/video/DGoods";
import {getDClassify, getlistByPid, listDClassify, listDClassifyVo} from "@/api/video/DClassify";

export default {
  name: "SoldDGoods",
  dicts: ['good_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      DGoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cid: null,
        cid2: null,
        goodName: null,
        downloadFlg: null,
        status: null,
        createUserName: null,
        beginTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单参数
      form2: {},
      form3: {},
      // 表单校验
      rules: {},
      rules2: {},
      rules3: {},
      classifyData:[],
      classifyData2:[],
      classifyAddData:[],
      // 日期范围
      dateRange: [],
    };
  },
  created() {
    this.getClassifyList();
    this.getList();
  },
  methods: {
    /**查询一级分类集合**/
    getClassifyList() {
      listDClassifyVo().then(response => {
        this.classifyData = response.rows;
      });
    },

    oneQuery(){
      const pid=this.queryParams.cid;
      console.log("pid："+pid)
      //this.ids = selection.map(item => item.id)
      getlistByPid(pid).then(response => {
        this.classifyData2 = response.data;
      });
      this.handleQuery();
    },

    oneHandle(){
      const pid=this.form.cid;
      getlistByPid(pid).then(response => {
        this.classifyAddData = response.data;
      });
    },
    //选择二级分类默认赋值
    twoHandle(){
      const id=this.form.cid2;
      getDClassify(id).then(response => {
        this.form.goodName = response.data.name;
        this.form.imageUrl = response.data.imageUrl;
      });
    },

    oneHandle2(){
      const pid=this.form2.cid;
      getlistByPid(pid).then(response => {
        this.classifyAddData = response.data;
      });
    },

    /** 查询商品列表 */
    getList() {
      this.loading = true;
      console.log(this.dateRange)
      if(this.dateRange.length>0){
        this.queryParams.beginTime=this.dateRange[0];
        this.queryParams.endTime=this.dateRange[1];
      }else{
        this.queryParams.beginTime=null;
        this.queryParams.endTime=null;
      }
      listSoldDGoods(this.queryParams).then(response => {
        this.DGoodsList = response.rows;

        this.DGoodsList.forEach((item,index)=> {
          if(item.imageUrl!=null){
            if(item.imageUrl.includes('iyuanchuang.cn')) {
              item.imageUrl = item.imageUrl.replace("iyuanchuang.cn", "iyuanchuang.com.cn")
            }
          }
        })

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.rules2();
    },
    // 取消按钮
    cancel3() {
      this.open3 = false;
      this.rules3();
    },
    // 表单重置
    reset() {
      this.dateRange = [];
      this.form = {
        id: null,
        cid: null,
        cid2: null,
        imageUrl: null,
        videoUrl: null,
        goodName: null,
        downloadFlg: null,
        status: null,
        createUserName: null,
        beginTime: null,
        endTime: null,
      };
      this.resetForm("form");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        id: null,
        cid: null,
        cid2: null
      };
      this.resetForm("form2");
    },
    // 表单重置
    reset3() {
      this.form3 = {
        id: null,
        status: null
      };
      this.resetForm("form3");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.beginTime=null;
      this.queryParams.endTime=null;
      console.log(this.dateRange)
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cid && this.form.cid2 && this.form.goodName && this.form.imageUrl && this.form.videoUrl && this.form.testTime && this.form.sellPrice) {
              if (this.form.id != null) {
                updateDGoods(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
               });
              } else {
                addDGoods(this.form).then(response => {
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                });
              }
          }else {
            this.$modal.msgSuccess("请输入完整的视频信息");
          }
        }
      });
    },

    /** 提交按钮 */
    submitForm2() {
      console.log("id："+this.form2.id)
      if (this.form2.id != null) {
        console.log("id："+this.form2.id)
        updateDGoodsBach(this.form2).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open2 = false;
          this.getList();
        });
      }
    },
    /** 提交按钮 */
    submitForm3() {
      console.log("id："+this.form3.id)
      if (this.form3.id != null) {
        updateDGoodsBach(this.form3).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open3 = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function() {
        return delDGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/DGoods/export', {
        ...this.queryParams
      }, `DGoods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
